package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"yzy6.26exam/api/request"
	"yzy6.26exam/golbal"
	"yzy6.26exam/internal/moble"
)

func CommitList(context *gin.Context) {
	var commit_list request.CommitList
	if err := context.ShouldBind(&commit_list); err != nil {
		context.JSON(http.StatusBadRequest, gin.H{
			"code":    204,
			"message": "解析失败",
			"data":    err.Error(),
		})
		return
	}
	var commits []struct {
		UserName  string
		UserImage string
		moble.Commit
	}
	if err := golbal.DB.Model(&moble.Commit{}).Joins("join users on commits.user_id = users.id").Select("commits.*,users.name as user_name,users.head_image as user_image").Order("commits.created_at desc").Scan(&commits).Error; err != nil {
		context.JSON(http.StatusOK, gin.H{
			"code":    203,
			"message": "评论获取失败",
			"data":    err.Error(),
		})
		return
	}
	context.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "评论获取成功",
		"data":    commits,
	})
	return

}
